package com.situ.util.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.util.ResponseData;
import com.situ.util.dto.CstSerSj;
import com.situ.util.entity.CstService;
import com.situ.util.entity.SysUser;
import com.situ.util.service.CstServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class CstSerControllers {
    @Autowired
    private CstServiceService cstServiceService;

    //高级分页模糊查询
    @RequestMapping(value = "queryServiceByLike", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryServiceByLike(CstSerSj value,
                                           @RequestParam(defaultValue ="1",required = false) Integer startPage ,
                                           @RequestParam(defaultValue = "5",required = false) Integer PageSize) {

        PageHelper.startPage(startPage,PageSize );
        List<CstService> list = cstServiceService.queryServiceByLike(value);
        PageInfo pageInfo=new PageInfo(list,5);


        return ResponseData.ok().putDataValue("pageInfo",pageInfo);
    }

    //高级分页模糊查询
    @RequestMapping(value = "queryServiceByLike2", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryServiceByLike2(CstSerSj value,
                                            @RequestParam(defaultValue ="1",required = false) int startPage ,
                                            @RequestParam(defaultValue = "5",required = false) int PageSize) {

        PageHelper.startPage(startPage,PageSize );
        List<CstService> list = cstServiceService.queryServiceByLike2(value);
        List<SysUser> use=cstServiceService.queryusers();
        PageInfo pageInfo=new PageInfo(list,5);


        return ResponseData.ok().putDataValue("pageInfo",pageInfo).putDataValue("use", use);
    }

    //高级分页模糊查询
    @RequestMapping(value = "queryServiceByLikeFk", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryServiceByLikeFk(CstSerSj value,
                                            @RequestParam(defaultValue ="1",required = false) int startPage ,
                                            @RequestParam(defaultValue = "5",required = false) int PageSize) {

        PageHelper.startPage(startPage,PageSize );
        List<CstService> list = cstServiceService.queryServiceByLikeFk(value);
        PageInfo pageInfo=new PageInfo(list,5);


        return ResponseData.ok().putDataValue("pageInfo",pageInfo);
    }

    //高级分页模糊查询
    @RequestMapping(value = "queryServiceByLikeGd", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryServiceByLikeGd(CstSerSj value,
                                             @RequestParam(defaultValue ="1",required = false) int startPage ,
                                             @RequestParam(defaultValue = "5",required = false) int PageSize) {

        PageHelper.startPage(startPage,PageSize );
        List<CstService> list = cstServiceService.queryServiceByLikeGd(value);
        PageInfo pageInfo=new PageInfo(list,5);


        return ResponseData.ok().putDataValue("pageInfo",pageInfo);
    }

    //时间转换器
    @InitBinder  //
    public void dateconvert(WebDataBinder bind){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        PropertyEditor n=new CustomDateEditor(format, true);
        bind.registerCustomEditor(Date.class, n);
    }

}
